// 新建窗口
import { ipcMain } from 'electron'
import { WindowCreator } from '../hooks/createdWindow'
let win: WindowCreator | null = null
const createWindow = (name: string) => {
  //判断是否创建过窗口,如果创建过置顶
  if (win && win.window && !win.window.isDestroyed()) {
    win.show()
    return
  }

  // 创建新窗口
  win = new WindowCreator({
    title: name || '音频窗口展示',
    width: 800,
    height: 600,
    frame: false,
    transparent: true,
    entry: 'index.html#/recordingPage'
  })
  win.show()
}
// 音频窗口ipc通信
function audioWindowIpc() {
  ipcMain.handle('audio-window', async (_, name: string) => {
    createWindow(name)
  })
  // 关闭窗口
  ipcMain.handle('audio-window-close', async (_) => {
    if (win && win.window && !win.window.isDestroyed()) {
      win?.window?.close()
      win = null
    }
  })
}
export default {
  audioWindowIpc
}
